#!/usr/bin/env bash

echo 清理内存

sync
sysctl -w vm.drop_caches=1
sysctl -w vm.drop_caches=2
sysctl -w vm.drop_caches=3


echo 清理无效镜像
docker images -q -f dangling=true | xargs -r docker rmi
docker volume ls -qf dangling=true | xargs -r docker volume rm
docker builder prune -f


echo 清理多余过期镜像

docker image ls|tail -n +2 > .docker-images-list.txt
for name in `cat .docker-images-list.txt |awk '{print $1}'|sort|uniq`
do
  for tag in `cat .docker-images-list.txt | grep -E "^$name "|awk '{printf"%s:%s\n",$1,$2}'|sort -V | head -n -2`
  do
    echo 删除 $tag
    docker rmi $tag
  done
done

docker builder prune -f


echo "清理完成!"